/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinker/icare">Thinker</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.com.thinker.security.sdk.system.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.thinker.security.sdk.system.dao.SystemUserDao;
import cn.com.thinker.security.sdk.system.entity.SystemUser;
import cn.com.thinker.security.sdk.system.utils.Digests;
import cn.com.thinker.security.sdk.system.utils.Encodes;


/**
 * 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 * @author Thinker
 * @version 2013-5-15
 */
@Service
@Transactional(readOnly = true)
public class SystemUserService {
	
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
//	public static final int SALT_SIZE = 8;
	
	@Autowired
	private SystemUserDao systemUserDao;
	
//	@Transactional(readOnly = true,rollbackFor=Exception.class) 
	public SystemUser getUserByLoginName(String loginName) {
		System.out.println("监控看看");
		if(null == systemUserDao) {
			System.out.println("空看看");
			
		}
		
		return systemUserDao.findByLoginName(loginName);
	}
	
	/**
	 * 验证密码
	 * @param plainPassword 明文密码
	 * @param password 密文密码
	 * @return 验证成功返回true
	 */
	public static boolean validatePassword(String plainPassword, String password) {
		byte[] salt = Encodes.decodeHex(password.substring(0,16));
		byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
		return password.equals(Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword));
	}


	
}
